package Algorithm;

import Structure.Graph;
import Structure.GraphGenerator;
import Structure.Node;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * @Author: ylc
 * @Description:广度优先遍历
 * @Date Created in 2022-06-06 17:07
 */
public class Bfs {
    public static void bfs(Node node) {
        if (node == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<>();
        Set<Node> set = new HashSet<>();
        queue.offer(node);
        set.add(node);
        while(!queue.isEmpty()){
            Node curNode = queue.poll();
            System.out.print(curNode.value + " ");
            for (Node next :
                    curNode.nexts) {
                if (!set.contains(next)) {
                    set.add(next);
                    queue.offer(next);
                }
            }
        }

    }
    public static void main(String[] args) {
        Graph graph = create();
        bfs(graph.nodes.get(1));
    }
    public static Graph create(){
        int[][] arr = {
                {1,1,2},{1,2,1},
                {1,1,3},{1,3,1},
                {1,1,4},{1,4,1},
                {1,2,3},{1,3,2},
                {1,3,4},{1,4,3},
                {1,2,5},{1,5,2},
                {1,4,5},{1,5,4}

        };

        return GraphGenerator.generateGraph(arr);
    }

}
